import request from '@/utils/request'
import { encryptedData } from '@/utils/encrypt'
import { loginRSA } from '@/config'

export async function login(data) {
  if (loginRSA) {
    data = await encryptedData(data)
  }
  return request({
    url: '/auth/login',
    method: 'post',
    data,
  })
}

export async function socialLogin(data) {
  if (loginRSA) {
    data = await encryptedData(data)
  }
  return request({
    url: '/socialLogin',
    method: 'post',
    data,
  })
}

export function getUserInfo() {
  return request({
    url: '/auth',
    method: 'post',
  })
}

export function logout() {
  return request({
    url: '/logout',
    method: 'get',
  })
}

export function register(data) {
  return request({
    url: '/register',
    method: 'post',
    data,
  })
}

export function getList(params) {
  return request({
    url: `/user`,
    method: 'get',
    params,
  })
}

export function getOne(id, params) {
  return request({
    url: `/user/${id}`,
    method: 'get',
    params,
  })
}

export function doAdd(data) {
  return request({
    url: `/user`,
    method: 'post',
    data,
  })
}

export function doEdit(id, data) {
  return request({
    url: `/user/${id}`,
    method: 'put',
    data,
  })
}

export function doDelete(id) {
  return request({
    url: `/user/${id}`,
    method: 'delete',
  })
}

export function doMultiDelete(ids) {
  return request({
    url: `/user/multidelete`,
    method: 'delete',
    data: { ids },
  })
}

export function updateOne(data, id) {
  return request({
    url: `/authUser/${id}`,
    method: 'patch',
    data,
  })
}

export function changePwd(data, id) {
  return request({
    url: `/authUser/${id}/changePwd`,
    method: 'patch',
    data,
  })
}
